package com.ufcg.lvc.server;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.ufcg.lvc.client.my.RandomString;

/**
 * 
 * Servlet para fazer o upload de arquivos para o servidor.
 * 
 * @author Larissa
 * 
 */
@SuppressWarnings("serial")
public class FileUploadServlet extends HttpServlet {
	ArrayList<String> nomesOriginaisFotos = new ArrayList<String>();

	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		System.out.println("cheguei");
		ServletFileUpload upload = new ServletFileUpload();
		System.out.println("passei");
		try {
			FileItemIterator iter = upload.getItemIterator(request);

			while (iter.hasNext()) {
				FileItemStream item = iter.next();
				String nome = item.getName();
				nomesOriginaisFotos.add(nome);

				RandomString r = new RandomString(20);
				String newName = r.nextString() + "1" + nome.substring(nome.length() - 4);
				if (item.isFormField()) {
					// if(item.getFieldName().compareTo("userID")==0){
					// userId = Streams.asString(item.openStream());
					// }
					// else if(item.getFieldName().compareTo("attempts")==0){
					// attempts = Streams.asString(item.openStream());
					// }
				} else {
					String name = item.getFieldName();
					String fileName = "";

					if (name.compareTo("fileUpload") == 0) {
						InputStream stream = item.openStream();

						fileName = "C:\\Users\\larissa\\workspaceNOV\\Foto__\\war\\Fotos\\" + newName;
						OutputStream output = new FileOutputStream(fileName);
						// String fileName2 = "C:/ServerArea/img/" + newName;
						// OutputStream output2 = new
						// FileOutputStream(fileName2);

						// Process the input stream
						ByteArrayOutputStream out = new ByteArrayOutputStream();
						out.writeTo(output);
						// ByteArrayOutputStream out2 = new
						// ByteArrayOutputStream();
						// out2.writeTo(output2);

						int len;
						byte[] buffer = new byte[8192];
						while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
							out.write(buffer, 0, len);
							output.write(buffer, 0, len);

							// out2.write(buffer, 0, len);
							// output2.write(buffer, 0, len);
						}

						int maxFileSize = 10 * (1024 * 1024); // 10 megs max
						if (out.size() > maxFileSize) {
							throw new RuntimeException("File is > than "
									+ maxFileSize);
						}
						output.close();
						// output2.close();
					}
					response.getWriter().println(fileName);
					response.getWriter().println(nome);
				}
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	
}
